package com.oa.ylms.apps.business.assess.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.oa.ylms.apps.business.assess.IAssessModelService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.assess.IAssessModelStates;
import com.oa.ylms.apps.dao.assess.IAssessModelDao;
import com.oa.ylms.apps.entity.assess.AssessModelValue;

public class AssessModelServiceImpl implements IAssessModelService {

	private IAssessModelDao dao;
	private AssessModelValue assessModel;

	public int addAssessModel(String modelName, String itemIds)
			throws Exception {
		assessModel.setModelName(modelName);
		assessModel.setItemIds(itemIds);
		assessModel.setStates(IDEDStates.ENABLE);
		return dao.addAssessModel(assessModel);
	}

	public String deleteAssessModel(String modelIds) throws Exception {

		String unDelIds = "";
		String[] arrayModelId = modelIds.split(",");
		for (String modelIdStr : arrayModelId) {
			int modelId = Integer.parseInt(modelIdStr);
			assessModel = queryAssessModelByModelId(modelId);
			assessModel.setStates(IAssessModelStates.DELETE);
			boolean result = dao.editAssessModel(assessModel);
			if (result == false) {
				unDelIds = modelId + ",";
				continue;
			}
		}
		return unDelIds;
	}

	public boolean deleteAssessModel(int modelId) throws Exception {
		assessModel = queryAssessModelByModelId(modelId);
		if (assessModel != null) {
			assessModel.setStates(IAssessModelStates.DELETE);
			return dao.editAssessModel(assessModel);
		}
		return false;
	}

	public int countTpages(String modelName, int rows) throws Exception {
		if (StringUtils.isNotEmpty(modelName)
				&& StringUtils.isNotBlank(modelName)) {
			assessModel.setModelName(modelName);
		}
		return dao.countTpages(assessModel, rows);
	}

	public int getTotalNumber() throws Exception {
		return dao.getTotalNumber();
	}

	public List<AssessModelValue> queryAssessModelWithPage(String modelName,
			int rows, int pageNo) throws Exception {
		if (StringUtils.isNotEmpty(modelName)
				&& StringUtils.isNotBlank(modelName)) {
			assessModel.setModelName(modelName);
		}
		return dao.queryAssessModelWithPage(assessModel, rows, pageNo);
	}

	public AssessModelValue queryAssessModelByModelId(int modelId)
			throws Exception {

		return dao.queryAssessModelByModelId(modelId);
	}

	public boolean deleteAssessItemInAssessModel(int itemId, int modelId)
			throws Exception {
		assessModel = queryAssessModelByModelId(modelId);
		if (assessModel != null) {
			String itemIds = "";
			if(assessModel.getItemIds().length()==1)
			{
				assessModel.setItemIds(itemIds);
			}
			else{
			String[] arrayItemIds = assessModel.getItemIds().split(",");	
			for (String itemIdStr : arrayItemIds) {
				int _itemId = Integer.parseInt(itemIdStr);
				if (_itemId != itemId) {
					itemIds = itemIds + _itemId + ",";
				}
			}
				itemIds = itemIds.substring(0, itemIds.length() - 1);
				assessModel.setItemIds(itemIds);
			
			}
			return dao.editAssessModel(assessModel);
		}
		return false;
	}

	public boolean toDisable(int modelId) throws Exception {
		assessModel = queryAssessModelByModelId(modelId);
		if (assessModel != null) {
			assessModel.setStates(IAssessModelStates.DISABLE);
			return dao.editAssessModel(assessModel);
		}
		return false;
	}

	public boolean toEnable(int modelId) throws Exception {
		assessModel = queryAssessModelByModelId(modelId);
		if (assessModel != null) {
			assessModel.setStates(IAssessModelStates.ENABLE);
			return dao.editAssessModel(assessModel);
		}
		return false;
	}

	public List<AssessModelValue> queryByModelName(AssessModelValue assessModel)
			throws Exception {

		return dao.queryByModelName(assessModel);
	}

	public IAssessModelDao getDao() {
		return dao;
	}

	public void setDao(IAssessModelDao dao) {
		this.dao = dao;
	}

	public AssessModelValue getAssessModel() {
		return assessModel;
	}

	public void setAssessModel(AssessModelValue assessModel) {
		this.assessModel = assessModel;
	}

	public List<AssessModelValue> queryAllAssessModel() throws Exception {
		return dao.queryAllAssessModel();
	}

}
